package db;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;

import com.netease.db.provider.BaseDBOpenHelper;
import com.netease.log.NTLog;
import com.netease.util.KeyValuePair;

import db.XoneDBTables.AccountTable;
import db.XoneDBTables.AppInfoTable;
import db.XoneDBTables.DiscussTipTable;
import db.XoneDBTables.DraftTable;
import db.XoneDBTables.MsgCenterTable;

public class XoneDBOpenHelper extends BaseDBOpenHelper {
	public static final String TAG = "XoneDBOpenHelper";
	
	public static String DataBaseName = "xone.db";
	private static final int DB_VERSION = 1;
	
	private static XoneDBOpenHelper instance = null;

	static public XoneDBOpenHelper getInstance(Context context) {
		if(instance == null)
		{
			instance = new XoneDBOpenHelper(context);
		}
		
		return instance;
	}
	
	public XoneDBOpenHelper(Context context) {
		super(context, DataBaseName, null, DB_VERSION);
	}

	@Override
	protected void onDestroyOldDB(SQLiteDatabase db) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		NTLog.i(TAG,"onCreate");
		onCreateAccountTable(db);
		onCreateMsgCenterTable(db);
		onCreateAppInfoTable(db);
		onCreateDiscussTipTable(db);
		onCreateDraftTable(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}
	private void onCreateAccountTable(SQLiteDatabase db) {
		List<KeyValuePair> columns = null;
		columns = new LinkedList<KeyValuePair>();
		columns.add(new KeyValuePair(BaseColumns._ID, INTEGER_KEY_AUTO_INC));
		columns.add(new KeyValuePair(AccountTable.C_USER_ACCOUNT, TEXT_NOT_NULL));
		columns.add(new KeyValuePair(AccountTable.C_USER_PASSWORD, TEXT));
		columns.add(new KeyValuePair(AccountTable.C_USER_ID, TEXT_NOT_NULL));
		columns.add(new KeyValuePair(AccountTable.C_USER_NICKNAME, TEXT));
		columns.add(new KeyValuePair(AccountTable.C_USER_AVATAR, TEXT));
		columns.add(new KeyValuePair(AccountTable.C_USER_TYPE, INT));
		columns.add(new KeyValuePair(AccountTable.C_LAST_LOGIN, INT));
		columns.add(new KeyValuePair(AccountTable.C_UPDATE_TIME, LONG));
		columns.add(new KeyValuePair(AccountTable.C_USER_ATTR, INT));
		createTable(db, AccountTable.TABLE_NAME, columns, 
				"UNIQUE (" + AccountTable.C_USER_ACCOUNT + ", " + AccountTable.C_USER_TYPE +  ") ON CONFLICT REPLACE");
	}
	
	
	private void onCreateMsgCenterTable(SQLiteDatabase db) {
        List<KeyValuePair> columns = null;
        columns = new LinkedList<KeyValuePair>();
        columns.add(new KeyValuePair(BaseColumns._ID, INTEGER_KEY_AUTO_INC));
        columns.add(new KeyValuePair(MsgCenterTable.C_ACCOUNT, TEXT_NOT_NULL));
        columns.add(new KeyValuePair(MsgCenterTable.C_MSGID, TEXT_NOT_NULL));
        columns.add(new KeyValuePair(MsgCenterTable.C_PUSH, TEXT));
        columns.add(new KeyValuePair(MsgCenterTable.C_MESSAGE, TEXT));
        columns.add(new KeyValuePair(MsgCenterTable.C_TYPE, INT));
        columns.add(new KeyValuePair(MsgCenterTable.C_READ, INT));
        columns.add(new KeyValuePair(MsgCenterTable.C_UPDATED, LONG));
        columns.add(new KeyValuePair(MsgCenterTable.C_RESERVE, TEXT));
        createTable(db, MsgCenterTable.TABLE_NAME, columns, null); 
        db.execSQL("CREATE INDEX msgIdIndex ON " + MsgCenterTable.TABLE_NAME  +" ("
                + MsgCenterTable.C_MSGID + ")");
    }
	
	private void onCreateAppInfoTable(SQLiteDatabase db) {
	    List<KeyValuePair> columns = null;
	    
	    columns = new LinkedList<KeyValuePair>();
        columns.add(new KeyValuePair(BaseColumns._ID, INTEGER_KEY_AUTO_INC));
        columns.add(new KeyValuePair(AppInfoTable.C_ACCOUNT, TEXT));
        columns.add(new KeyValuePair(AppInfoTable.C_INFOGROUP, LONG));
        columns.add(new KeyValuePair(AppInfoTable.C_SORTTYPE, LONG));
        columns.add(new KeyValuePair(AppInfoTable.C_INFOID, TEXT));
        columns.add(new KeyValuePair(AppInfoTable.C_TIME, LONG));
        columns.add(new KeyValuePair(AppInfoTable.C_JSON, TEXT));
        createTable(db, AppInfoTable.TABLE_NAME, columns, null);
    }

	private void onCreateDiscussTipTable(SQLiteDatabase db) {
	    List<KeyValuePair> columns = null;
	    
	    columns = new LinkedList<KeyValuePair>();
        columns.add(new KeyValuePair(BaseColumns._ID, INTEGER_KEY_AUTO_INC));
        columns.add(new KeyValuePair(DiscussTipTable.C_TYPE, INT));
        columns.add(new KeyValuePair(DiscussTipTable.C_SUBTYPE, INT));
        columns.add(new KeyValuePair(DiscussTipTable.C_TIME, LONG));
        columns.add(new KeyValuePair(DiscussTipTable.C_PINYIN, TEXT));
        columns.add(new KeyValuePair(DiscussTipTable.C_CONTENT, TEXT));
        columns.add(new KeyValuePair(DiscussTipTable.C_JSON, TEXT));
        createTable(db, DiscussTipTable.TABLE_NAME, columns, "UNIQUE (" + DiscussTipTable.C_CONTENT +  ") ON CONFLICT REPLACE");
	}
	
	private void onCreateDraftTable(SQLiteDatabase db) {
	    List<KeyValuePair> columns = null;
        
        columns = new LinkedList<KeyValuePair>();
        columns.add(new KeyValuePair(BaseColumns._ID, INTEGER_KEY_AUTO_INC));
        columns.add(new KeyValuePair(DraftTable.C_ACCOUNT, TEXT));
        columns.add(new KeyValuePair(DraftTable.C_CONTENT, TEXT));
        columns.add(new KeyValuePair(DraftTable.C_RECORDER, TEXT));
        columns.add(new KeyValuePair(DraftTable.C_DURATION, INT));
        columns.add(new KeyValuePair(DraftTable.C_IMAGE, TEXT));
        columns.add(new KeyValuePair(DraftTable.C_FILTER, INT));
        columns.add(new KeyValuePair(DraftTable.C_TIME, LONG));
        createTable(db, DraftTable.TABLE_NAME, columns, null);
    }
}
